function e(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

function t(e, t) {
    if (!(e instanceof t)) throw new TypeError("Cannot call a class as a function");
}

Object.defineProperty(exports, "__esModule", {
    value: !0
});

var r = function() {
    function e(e, t) {
        for (var r = 0; r < t.length; r++) {
            var s = t[r];
            s.enumerable = s.enumerable || !1, s.configurable = !0, "value" in s && (s.writable = !0), 
            Object.defineProperty(e, s.key, s);
        }
    }
    return function(t, r, s) {
        return r && e(t.prototype, r), s && e(t, s), t;
    };
}(), s = e(require("../utils/event.js")), i = e(require("../model/user.js")), a = e(require("../model/stream.js")), u = require("../common/constants.js"), n = "UserController", o = function() {
    function e(r) {
        t(this, e), this.userMap = new Map(), this.userList = [], this.streamList = [], 
        this._emitter = new s.default(), this.componentContext = r, this.isNewVersion = r.isNewVersion;
    }
    return r(e, [ {
        key: "userEventHandler",
        value: function(e) {
            var t = e.detail.code, r = void 0;
            if (!e.detail.message || "string" != typeof e.detail.message) return console.warn(n, "userEventHandler 数据格式错误"), 
            !1;
            try {
                r = JSON.parse(e.detail.message);
            } catch (e) {
                return console.warn(n, "userEventHandler 数据格式错误", e), !1;
            }
            switch (t) {
              case 1020:
                this.isNewVersion;
                break;

              case 1031:
                this.addUser(r);
                break;

              case 1032:
                this.removeUser(r);
                break;

              case 1033:
                this.updateUserVideo(r);
                break;

              case 1034:
                this.updateUserAudio(r);
            }
        }
    }, {
        key: "addUser",
        value: function(e) {
            var t = this, r = e.userlist, s = this.userMap;
            Array.isArray(r) && r.length > 0 && r.forEach(function(e) {
                var r = e.userid, a = t.getUser(r);
                a || (a = new i.default({
                    userID: r
                }), t.userList.push({
                    userID: r
                })), s.set(r, a), t._emitter.emit(u.EVENT.REMOTE_USER_JOIN, {
                    userID: r,
                    userList: t.userList
                });
            });
        }
    }, {
        key: "removeUser",
        value: function(e) {
            var t = this, r = e.userlist;
            Array.isArray(r) && r.length > 0 && r.forEach(function(e) {
                var r = e.userid, s = t.getUser(r);
                t._removeUserAndStream(r), s.streams.main && s.streams.main.reset(), s.streams.aux && s.streams.aux.reset(), 
                t._emitter.emit(u.EVENT.REMOTE_USER_LEAVE, {
                    userID: r,
                    userList: t.userList,
                    streamList: t.streamList
                }), s = void 0, t.userMap.delete(r);
            });
        }
    }, {
        key: "updateUserVideo",
        value: function(e) {
            var t = this;
            console.log(n, "updateUserVideo", e);
            var r = e.userlist;
            Array.isArray(r) && r.length > 0 && r.forEach(function(e) {
                var r = e.userid, s = e.streamtype, i = r + "_" + s, o = e.hasvideo, m = e.playurl, l = t.getUser(r);
                if (l) {
                    var c = l.streams[s];
                    console.log(n, "updateUserVideo start", l, s, c), c ? c.setProperty({
                        hasVideo: o
                    }) : (l.streams[s] = c = new a.default({
                        userID: r,
                        streamID: i,
                        hasVideo: o,
                        src: m,
                        streamType: s
                    }), t._addStream(c)), "aux" === s && (o ? (c.objectFit = "contain", t._addStream(c)) : t._removeStream(c)), 
                    t.userList.find(function(e) {
                        if (e.userID === r) return e["has" + s.replace(/^\S/, function(e) {
                            return e.toUpperCase();
                        }) + "Video"] = o, !0;
                    }), console.log(n, "updateUserVideo end", l, s, c);
                    var d = o ? u.EVENT.REMOTE_VIDEO_ADD : u.EVENT.REMOTE_VIDEO_REMOVE;
                    t._emitter.emit(d, {
                        stream: c,
                        streamList: t.streamList,
                        userList: t.userList
                    });
                }
            });
        }
    }, {
        key: "updateUserAudio",
        value: function(e) {
            var t = this, r = e.userlist;
            Array.isArray(r) && r.length > 0 && r.forEach(function(e) {
                var r = e.userid, s = r + "_main", i = e.hasaudio, n = e.playurl, o = t.getUser(r);
                if (o) {
                    var m = o.streams.main;
                    m ? m.setProperty({
                        hasAudio: i
                    }) : (o.streams.main = m = new a.default({
                        userID: r,
                        streamID: s,
                        hasAudio: i,
                        src: n,
                        streamType: "main"
                    }), t._addStream(m)), t.userList.find(function(e) {
                        if (e.userID === r) return e["has" + "main".replace(/^\S/, function(e) {
                            return e.toUpperCase();
                        }) + "Audio"] = i, !0;
                    });
                    var l = i ? u.EVENT.REMOTE_AUDIO_ADD : u.EVENT.REMOTE_AUDIO_REMOVE;
                    t._emitter.emit(l, {
                        stream: m,
                        streamList: t.streamList,
                        userList: t.userList
                    });
                }
            });
        }
    }, {
        key: "getUser",
        value: function(e) {
            return this.userMap.get(e);
        }
    }, {
        key: "getStream",
        value: function(e) {
            var t = e.userID, r = e.streamType, s = this.userMap.get(t);
            if (s) return s.streams[r];
        }
    }, {
        key: "getUserList",
        value: function() {
            return this.userList;
        }
    }, {
        key: "getStreamList",
        value: function() {
            return this.streamList;
        }
    }, {
        key: "reset",
        value: function() {
            return this.streamList.forEach(function(e) {
                e.reset();
            }), this.streamList = [], this.userList = [], this.userMap.clear(), {
                userList: this.userList,
                streamList: this.streamList
            };
        }
    }, {
        key: "on",
        value: function(e, t, r) {
            this._emitter.on(e, t, r);
        }
    }, {
        key: "off",
        value: function(e, t) {
            this._emitter.off(e, t);
        }
    }, {
        key: "_removeUserAndStream",
        value: function(e) {
            this.streamList = this.streamList.filter(function(t) {
                return t.userID !== e && "" !== t.userID;
            }), this.userList = this.userList.filter(function(t) {
                return t.userID !== e;
            });
        }
    }, {
        key: "_addStream",
        value: function(e) {
            this.streamList.includes(e) || this.streamList.push(e);
        }
    }, {
        key: "_removeStream",
        value: function(e) {
            this.streamList = this.streamList.filter(function(t) {
                return t.userID !== e.userID || t.streamType !== e.streamType;
            }), this.getUser(e.userID).streams[e.streamType] = void 0;
        }
    } ]), e;
}();

exports.default = o;